<?php
namespace app\home\model;

use think\Db;
use think\exception\PDOException;
use think\Model;

class Visiting extends Model{
    protected  $name ="visiting";

    //動態數據
    public function selectColumn($time,$guan,$explain){
//        dump($time);
//        $res_array = [];
//        if($explain==1){
//            $result =$this->where(['venue'=>"$guan","status"=>1,'day'=>$time,'explain'=>1])->column("between_time");
////        dump($result);
//            foreach($result as $k=>$value){
//                $sum  = $this->where(['venue'=>"$guan",'day'=>$time , 'between_time'=>$value,'explain'=>1])->sum("views");
//                if($sum >= 100){
//                    $res_array[] = $value;
//                }
//                unset($sum);
//            }
////        dump($result);
////        dump(array_unique($res_array));
////       return $this->where(['venue'=>"$guan","status"=>1,'day'=>$time])->column("between_time");//
//          return array_unique($res_array);
//        }else{
//          return $res_array;
//        }
//
//
//

//        if($explain==1){
//            return $this->where(['venue'=>"$guan","status"=>1,'day'=>$time])->column("between_time");
//        }else{
//            return [];
//        }

        $timer = [];
        if($explain==1){
            $dinner1  =  $this->where(['venue'=>"$guan","status"=>1,'day'=>$time,'dinner'=>0,'explain'=>1])->count("dinner");
            $dinner2  =  $this->where(['venue'=>"$guan","status"=>1,'day'=>$time,'dinner'=>1,'explain'=>1])->count("dinner");
//            dump($dinner1);
            if($dinner1 >= 2){
                $timer[] = 0;
            }
            if($dinner2 >= 2){
                $timer[] = 1;
            }
            return  $timer ;
        }else{
            return [];
        }



    }

    //手机端查看
    public function selectMobileColumn(){
        return    $this->where()->column("between_time");
    }
    //全都要
    public function SelectAll($id){
        return   $this->where("id",$id)->find();
    }
    //添加
    public function addeditVisit($param){
        try {
         $param['user_id']=2;
          $result = $this->create($param);
            // 验证失败 输出错误信息
            return info("增加成功", 1);
        } catch (PDOException $e) {
            return info($e->getMessage(), 2 );
        }
    }
    //修改
    public function editVisit($param){
        try {
            $result = self::save($param,["id"=>$param]);
            // 验证失败 输出错误信息
            return info("增加成功", 1);
        } catch (PDOException $e) {

            return info($e->getMessage(), 2 );
        }
    }

    public function mobileSelect($data){
         $mobile=Session("mobile");
         $list = $this->where("mobile",$mobile)->order('id desc')->select();
         $time = config('visit_time2');
             foreach($list as $key=>$value){
                 $list[$key]['between_time']=$time[$value['between_time']];
             }
         return $list;
    }
    public function geren($data){
        $list = $this->where("mobile",$data)->order("id desc")->select();
        $time = config('visit_time2');
        foreach($list as $key=>$value){
            $list[$key]['between_time']=$time[$value['between_time']];
        }
        return $list;
    }
    //取消预约
    public function delYuyue($param){
        try{
            $result = self::save(["status"=>3,"reason"=>$param['reason']],["id"=>$param['id']]);
            return info("取消成功",1,"myOrder");
        }catch (PDOException $e){
            return info($e->getMessage(), 2,"myOrder");
        }
    }


}
